package cn.heycm.tenant.user.impl;

import cn.heycm.tenant.common.annotation.Tenant;
import cn.heycm.tenant.user.UserService;
import cn.heycm.tenant.user.repository.UserRepository;
import cn.heycm.tenant.user.repository.entity.UserProfile;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author hey
 * @version 1.0
 * @since 2024/8/25 13:00
 */
@Service
@Slf4j
@AllArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserRepository repository;

    @Override
    public String defaultTenant(Integer userId) {
        UserProfile profile = repository.getById(userId);
        if (null == profile) {
            return "啥都没有";
        }
        return profile.toString();
    }

    @Override
    @Tenant("tenant2")
    public String fixedTenant(Integer userId) {
        UserProfile profile = repository.getById(userId);
        if (null == profile) {
            return "啥都没有";
        }
        return profile.toString();
    }

    @Override
    @Tenant
    public String headerTenant(Integer userId) {
        UserProfile profile = repository.getById(userId);
        if (null == profile) {
            return "啥都没有";
        }
        return profile.toString();
    }

    @Override
    @Tenant
    public String transaction(String name) {
        repository.addName(name);
        return "ok";
    }
}
